﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App2
{
    public partial class Form1 : Form
    {
        #region Field
        private Chess myChess;
        private List<ChessMan> chessManList;
        private int X0 = 100;    //横向起始坐标
        private int Y0 = 100;    //竖向起始坐标
        private int Step = 50;   //按钮间距
        private int CmWidth = 45;    //按钮宽度
        private ChessMan hiddenCm;//隐藏按钮

        public Form1()
        {
            InitializeComponent();
        }
        #endregion
        #region Event
        //开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            myChess = new Chess();
            myChess.Init();
            InitChessMan();
        }

        //点击按钮
        private void ChessMan_Click(object sender, EventArgs e)
        {
            ChessMan cm = sender as ChessMan;
            int differ = Math.Abs(cm.Index - hiddenCm.Index);
            bool sameRowNeighbor = differ == 1 && (cm.Index / Chess.N == hiddenCm.Index / Chess.N);
            if (sameRowNeighbor || differ == Chess.N)
            {
                //交换按钮的text
                string str = hiddenCm.Text;
                hiddenCm.Text = cm.Text;
                cm.Text = str;

                this.hiddenCm.Visible = true;
                this.hiddenCm.Select();

                this.hiddenCm = cm;
                this.hiddenCm.Visible = false;
            }
            if (GameOver())
            {
                MessageBox.Show("游戏结束");
            }
        }
        #endregion

        #region Method
        //初始化所有按钮
        private void InitChessMan()
        {
            if (this.chessManList == null)
            {
                //添加按钮
                chessManList = new List<ChessMan>();
                for (int i = 0; i < Chess.GetTotalCount(); i++)
                {
                    ChessMan cm = new ChessMan();
                    cm.Text = (i + 1).ToString();
                    cm.RowIndex = i / Chess.N;
                    cm.ColumnIndex = i % Chess.N;
                    cm.Index = i;
                    cm.Width = CmWidth;
                    cm.Height = CmWidth;
                    cm.Top = Y0 + cm.RowIndex * Step;
                    cm.Left = X0 + cm.ColumnIndex * Step;
                    chessManList.Add(cm);
                    cm.Click += new EventHandler(ChessMan_Click);
                    this.Controls.Add(cm);
                }
            }
            //打乱按钮
            for (int i = 0; i < chessManList.Count; i++)
            {
                chessManList[i].Text = (this.myChess.GetNumber(i) + 1).ToString();
                if (this.myChess.GetNumber(i) == Chess.GetTotalCount() - 1)
                {
                    chessManList[i].Visible = false;
                    hiddenCm = chessManList[i];
                }
                else
                {
                    chessManList[i].Visible = true;
                }
            }
        }

        //游戏结束
        private bool GameOver()
        {
            for (int i = 0; i < chessManList.Count; i++)
            {
                if (chessManList[i].Text != (i + 1).ToString())
                {
                    return false;
                }
            }
            return true;
        }
        #endregion
    }

    public class ChessMan : Button
    {
        public int RowIndex { get; set; }
        public int ColumnIndex { get; set; }
        public int Index { get; set; }
    }
}
